'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const l = require('./compose/composer.cjs'),
  m = require('./doc/Document.cjs'),
  f = require('./errors.cjs'),
  p = require('./log.cjs'),
  y = require('./parse/line-counter.cjs'),
  u = require('./parse/parser.cjs');
function a(t) {
  const e = t.prettyErrors !== !1;
  return {
    lineCounter: t.lineCounter || (e && new y.LineCounter()) || null,
    prettyErrors: e,
  };
}
function g(t, e = {}) {
  const { lineCounter: r, prettyErrors: s } = a(e),
    n = new u.Parser(r == null ? void 0 : r.addNewLine),
    i = new l.Composer(e),
    o = Array.from(i.compose(n.parse(t)));
  if (s && r)
    for (const c of o)
      c.errors.forEach(f.prettifyError(t, r)),
        c.warnings.forEach(f.prettifyError(t, r));
  return o.length > 0 ? o : Object.assign([], { empty: !0 }, i.streamInfo());
}
function d(t, e = {}) {
  const { lineCounter: r, prettyErrors: s } = a(e),
    n = new u.Parser(r == null ? void 0 : r.addNewLine),
    i = new l.Composer(e);
  let o = null;
  for (const c of i.compose(n.parse(t), !0, t.length))
    if (!o) o = c;
    else if (o.options.logLevel !== 'silent') {
      o.errors.push(
        new f.YAMLParseError(
          c.range.slice(0, 2),
          'MULTIPLE_DOCS',
          'Source contains multiple documents; please use YAML.parseAllDocuments()',
        ),
      );
      break;
    }
  return (
    s &&
      r &&
      (o.errors.forEach(f.prettifyError(t, r)),
      o.warnings.forEach(f.prettifyError(t, r))),
    o
  );
}
function E(t, e, r) {
  let s;
  typeof e == 'function'
    ? (s = e)
    : r === void 0 && e && typeof e == 'object' && (r = e);
  const n = d(t, r);
  if (!n) return null;
  if (
    (n.warnings.forEach(i => p.warn(n.options.logLevel, i)),
    n.errors.length > 0)
  ) {
    if (n.options.logLevel !== 'silent') throw n.errors[0];
    n.errors = [];
  }
  return n.toJS(Object.assign({ reviver: s }, r));
}
function w(t, e, r) {
  let s = null;
  if (
    (typeof e == 'function' || Array.isArray(e)
      ? (s = e)
      : r === void 0 && e && (r = e),
    typeof r == 'string' && (r = r.length),
    typeof r == 'number')
  ) {
    const n = Math.round(r);
    r = n < 1 ? void 0 : n > 8 ? { indent: 8 } : { indent: n };
  }
  if (t === void 0) {
    const { keepUndefined: n } = r ?? e ?? {};
    if (!n) return;
  }
  return new m.Document(t, s, r).toString(r);
}
exports.parse = E;
exports.parseAllDocuments = g;
exports.parseDocument = d;
exports.stringify = w;
